package com.example.springboot.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 队列属性复制
 *
 * <p>
 * @author Zhang.Z
 * @version 2025-09-20
 */
public final class BeanUtils<T extends Serializable> {

    private static final Logger logger = LoggerFactory.getLogger(BeanUtils.class);

    private BeanUtils() {
    }

    /**
     * @param source
     * @param clazz
     * @return
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static <T> T copyProperties(Object source, Class<T> clazz) {
        if (source == null) {
            return null;
        }
        T t = null;
        try {
            t = clazz.newInstance();
            org.springframework.beans.BeanUtils.copyProperties(source, t);
        } catch (Exception e) {
            logger.error("请求失败", e);
        }
        return t;
    }

    /**
     * @param source
     * @param clazz
     * @return
     */
    public static <T> List<T> copyProperties(List<?> source, Class<T> clazz) {
        if (source == null || source.size() == 0) {
            return Collections.emptyList();
        }
        List<T> res = new ArrayList<>(source.size());
        for (Object o : source) {
            T t = null;
            try {
                t = clazz.newInstance();
                org.springframework.beans.BeanUtils.copyProperties(o, t);
            } catch (Exception e) {
                logger.error("请求失败", e);
            }
            res.add(t);
        }
        return res;
    }

}
